<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <!-- 函数累加器 -->
    <!-- 不依赖于外部变量，函数可以反复执行的累加器 -->
    <!-- <script>

        function add() {
            var count = 0;
            function demo(){
                count ++;
                console.log(count);
            }
            return demo;
        }

        var counter = add();
        counter();
        counter();
        counter();
        counter();
        counter();
        counter();
        counter();

    </script> -->

    <script>


        function add() {
            var count = 0;
            counter = function demo() {
                count++;
                console.log(count);
            }
        }

        var counter;
        add();
        counter();
        counter();
        counter();
        counter();
        counter();
        counter();
        counter();

    </script>

    <!-- <script>
        function test() {
            var num = 100;  
            function a () {
                num ++;
                console.log(num);
            }
            function b () {
                num --;
                console.log(num);
            }
            return [a, b];
        }
        var myArr = test();
        myArr[0]();
        myArr[1]();


        // function eater() {
        //     var food = ""; // 当缓存用
        //     var obj = {   // 只有函数可以产生域，即作用域
        //         eat: function () {
        //             if (food != "") {
        //                 console.log("I am eating " + food);
        //                 food = "";
        //             }else{
        //                 console.log("There is nothing!");
        //             }

        //         },
        //         push: function (myFood) {
        //             food = myFood;
        //             console.log(obj.push);
        //         }
        //     }
        //     // console.log(obj);
        //     return obj;
        // }

        // var eater1 = eater();
        // eater1.push('banana');
        // eater1.eat();
        // eater1.eat();
        // eater1.push('apple');
        // eater1.eat();
    </script> -->
</body>

</html>